var express=require("express");
var router=express.Router();
var crud=require("./crud");
var url=require("url");

router.all("*",function(req,res,next){
    res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
	res.header("X-Powered-By", ' 3.2.1');
	res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
router.post("/add",function(req,res){
    var username=req.body.username;
    var password=req.body.password;

    var sql="insert into user values (null,?,?)";
    var arr=[username,password];

    crud.insertData(sql,arr,function(result){
        if(result){
            res.json(200,{
                "msg":"success"
            })
        }else{
            res.json(200,{
                "msg":"error"
            })
        }
    });
});
router.post("/sel",function(req,res){
    var username=req.body.username;
    var password=req.body.password;
    

    var sql=" select * from  user where username=? && password=? ";
    var arr=[username,password];

    crud.selectData(sql,arr,function(result){
        if(result.length>0){
            res.json(200,result)
        }else{
            res.json(200,"查询失败")
        }
    });
});
router.post("/update",function(req,res){
    var username=req.body.username;
    var password=req.body.password;

    var sql=" update user set password=? where username=? ";
    var arr=[password,username];

    crud.selectData(sql,arr,function(result){
        if(result){
            res.json(200,{
                "msg":"success"
            })
        }else{
            res.json(200,{
                "msg":"error"
            })
        }
    });
});

module.exports=router;